package khfx.dao.basicdata.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import khfx.dao.BasicDao;
import khfx.dao.basicdata.B_BiaoWaiYeWuHeTongDao;
import khfx.dao.db.ResultSetHandler;
import khfx.dao.db.SQLQuery;
import khfx.dao.db.Session;
import khfx.util.Constant;
import khfx.util.StringTool;

import org.springframework.stereotype.Component;

@Component("biaoWaiYeWuHeTongDao")
public class B_BiaoWaiYeWuHeTongDaoImpl extends BasicDao implements B_BiaoWaiYeWuHeTongDao {
	public void insert(Session session, Integer reportDate) {
		StringBuffer sql = new StringBuffer();
		sql.append("insert into D_BIAOWAIYEWUHETONG (REPORTDATE, BANKCODE, CUSTOMERNAME, CUSTOMERCODE,CONTRACTNUMBER, BUSINESSBALANCE, MARGINAMOUNT) ")
			.append(" select REPORTDATE, BANKCODE, CUSTOMERNAME, CUSTOMERCODE,CONTRACTNUMBER, sum(BUSINESSBALANCE) BUSINESSBALANCE, sum(MARGINAMOUNT) MARGINAMOUNT ")
			.append(" from C_CREDIT_OFFBALANCE where REPORTDATE = ? ")
//			.append("  and REPORTDATE is not null and CUSTOMERNAME is not null and  CUSTOMERCODE is not null and CONTRACTNUMBER is not null ")
			.append(" group by REPORTDATE, BANKCODE, CUSTOMERNAME, CUSTOMERCODE,CONTRACTNUMBER ");
		update(session, sql.toString(), reportDate);
	}

	public void writeFile(Session session, Integer reportDate) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select REPORTDATE, BANKCODE, CUSTOMERNAME, CUSTOMERCODE,CONTRACTNUMBER, sum(BUSINESSBALANCE) BUSINESSBALANCE, sum(MARGINAMOUNT) MARGINAMOUNT ")
			.append(" from C_CREDIT_OFFBALANCE where REPORTDATE = ? ")
//			.append("  and REPORTDATE is not null and CUSTOMERNAME is not null and  CUSTOMERCODE is not null and CONTRACTNUMBER is not null ")
			.append(" group by REPORTDATE, BANKCODE, CUSTOMERNAME, CUSTOMERCODE,CONTRACTNUMBER ");
		
		SQLQuery query = session.createSQLQuery(sql.toString());
		query.setParameter(1, reportDate); 
		
		query.executeQuery(new ResultSetHandler(){
			public int process(ResultSet rs) throws SQLException {
				FileOutputStream fos = null;
				String srcFile = Constant.FILE_BWYWHT;
				try {
					File f = new File(srcFile);
					if(!f.exists()){
						File path = f.getParentFile();
						if(!path.exists())
							path.mkdirs();
						f.createNewFile();
					}
						
					fos = new FileOutputStream(srcFile,true);
					while(rs.next()){
						String temp = StringTool.getContent(null, rs.getInt("REPORTDATE"),
								rs.getString("BANKCODE"),
								rs.getString("CUSTOMERNAME"),
								rs.getString("CUSTOMERCODE"),
								rs.getString("CONTRACTNUMBER"),
								rs.getBigDecimal("BUSINESSBALANCE"),
								rs.getBigDecimal("MARGINAMOUNT")
						);
						byte[] b =temp.getBytes();
						fos.write(b,0,b.length);
					}
					fos.flush();
				} catch (Exception e) {
					throw new RuntimeException(e);
				} finally{
					if(fos!=null)
						try {
							fos.close();
						} catch (IOException e) {}
					
				}
				return 0;
			}
			
		});
	}

	public void batch(Session session, List<String> list) {
		String sql = "insert into D_BIAOWAIYEWUHETONG (REPORTDATE, BANKCODE, CUSTOMERNAME, CUSTOMERCODE,CONTRACTNUMBER, BUSINESSBALANCE, MARGINAMOUNT)  values (?,?,?,?,?,?,?)";
		Object[][] values = new Object[list.size()][7];
		int i =0;
		for(String str : list){
			String[] sm = str.split("\\"+StringTool.SPLIT);
			sm[3] = StringTool.getStr(sm[3]);
			sm[4] = StringTool.getStr(sm[4]);
			sm[5] = StringTool.getStr(sm[5]);
			sm[6] = StringTool.getStr(sm[6]);
			values[i] =  sm;
			i++;
		}
			batchLastUpdate(session, sql, values);
		
	}
	
}
